#--------------------------------------------------#
#   Academic freedom and the onset of autocratization   #
#--------------------------------------------------#

# Title: "Academic freedom and the onset of autocratization" #
# Authors: "Pelke, Lars", FAU Erlangen-Nürnberg
# date: 2023-04-24
# journal: Democratization
# DOI: 10.1080/13510347.2023.2207213
# written under "R version 4.2.3 (2023-03-15)"

#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# set working directory
#setwd("P:/PIPM/7. VWS Projekt Academic Freedom Index/replication_files_autocratization_and_academic_freedom")

#functions 
mean.new <- function(v) {
  if (all(is.na(v))) { return(NA) } else { return(mean(v, na.rm=T)) }
}

#### Load data ####

vdem_data <- readRDS("data/vdem_data.rds") 
vdem_cohort_5 <- readRDS("data/vdem_cohort_5.rds") 
vdem_cohort_10 <- readRDS("data/vdem_cohort_10.rds") 
wvsdata <- readRDS("data/wvsdata_prepared.rds") 


#### Merging Data with period effect and cohort effects ####

merged_data <- wvsdata %>%
  drop_na(iso3n, year)

merged_data <- merged_data %>%
  left_join(vdem_data, c("iso3n", "year")) 

merged_data <- merged_data %>%
  left_join(vdem_cohort_5, c("country_id", "cohortmatch5_20"))

merged_data <- merged_data %>%
  left_join(vdem_cohort_10, c("country_id", "cohortmatch10_20"))

merged_data <- merged_data %>%
  drop_na(iso3n, cohortmatch5_20)

saveRDS(merged_data, file = "data/merged_data_master.rds")




